/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-09-02
 * Time: 19:24
 */
public class demo1 {
    public static int flag = 0;
    public static Object locker = new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            for(int i = 0; i < 10; i++){
                synchronized (locker){
                    while(flag != 0){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("A");
                    flag = 1;
                    locker.notifyAll();
                }
            }
        });
        Thread t2 = new Thread(()->{
            for(int i = 0; i < 10; i++){
                synchronized (locker){
                    while (flag != 1){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("B");
                    flag = 2;
                    locker.notifyAll();
                }

            }
        });
        Thread t3 = new Thread(()->{
           for(int i = 0; i < 10; i++){
               synchronized (locker){
                   while(flag != 2){
                       try {
                           locker.wait();
                       } catch (InterruptedException e) {
                           throw new RuntimeException(e);
                       }
                   }
                   System.out.println("C");
                   flag = 0;
                   locker.notifyAll();
               }
           }
        });
        t1.start();
        t2.start();
        t3.start();

    }
}
